<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <h1>修改密码</h1>

    <input type="text" class="password">
    <br>
    <input type="text" class="newPwd">
    <br>
    <button class="btn">确认修改</button>
    <script>


      
        const oPwd = document.querySelector('.password') ;
        const oNewpwd = document.querySelector('.newPwd') ;
        const oBtn = document.querySelector('.btn') ;


        const token = localStorage.getItem('token') ;
        const id = localStorage.getItem('id');

        oBtn.onclick = function() {
            const password = oPwd.value ;
            const newPwd = oNewpwd.value ;
            const xhr2 = new XMLHttpRequest();

            xhr2.open('post', '/api/users/rpwd', true);
            // token是通过请求头携带的 ********************************************
            xhr2.setRequestHeader('authorization' , token) ;
            xhr2.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr2.send(`password=${password}&newPwd=${newPwd}&id=${id}`);
            // xhr2.send() ;

            xhr2.onreadystatechange = function () {
                if (xhr2.readyState === 4 && xhr2.status === 200) {
                    console.log(xhr2.responseText);
                    // 修改密码成功就跳转回首页
                }
            }
        }

        
       

    </script>
    
</body>
</html>